<?php

namespace control;
/*
*钢瓶检验记录
*/

class inspectSteelCylinderLogClass extends adIndexClass
{
    private const page = 'pastSteelCylinder'; # 权限
    private const table = 'steel_cylinder_log'; # 数据表
    private const title = '钢瓶检验记录';
    private const stateOption = ['检验中', '已检验'];
    private const tableKehu        = 'kehu';

    /**
     * 字段初始化
     */
    public function fieldInit(): array
    {
        $tr = [
            'barcode' => [
                'name'  => '内部编码',
                'width' => 200,
                'sort' => 1,
            ],
            'code' => [
                'name'  => '出厂码',
                'width' => 200,
                'sort' => 1,
            ],
            'model'  => [
                'name'  => '规格型号',
                'width' => 200,
            ],
            'state'          => [
                'name'  => '状态',
                'width' => 150,
            ],
            'nextTime'         => [
                'name'  => '检验日期',
                'width' => 200,
            ],
        ];
        return $tr;
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    '安检码'   => text(array("name" => "barcode")),
                    '出厂码'   => text(array("name" => "code")),
                    '规格型号'  => text(array("name" => "model")),
                ],
            ]
        ],1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    /*销售合同入口页*/
    public function home()
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        if ($this->power(self::page, "export")) {
            $button .="<span class='spanButton' onclick=\"layer('importTemplate','exportFrame',{cla:'".self::page."'})\">导出</span>";
        }
//        if ($this->power(self::page, "edit")) {
//            $button .= "<span class='spanButton' onclick=\"layer('" . self::page . "','edit',$('form[name=listForm]').serialize())\">检验完成</span>\n";
//        }
        //返回
        return $this->homeFun($this->search($html, layout: 'pop', toolButton: $button));
    }

    /*销售合同列表*/
    public function homeSearch()
    {
        //查询结果
        $where = ' 1=1 ';
        $equal = [
            'orderNo' => $this->post('orderNo'),
            'model' => $this->post('model'),
            'khid' => $this->post('khid'),
            'state' => $this->post('state'),
            'coid'=>self::$coid,
        ];
        //生成查询语句
        $where .= search([
            "=" => $equal,
        ]);
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'time',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page(["table" => self::table, "where" => $where, "order" => $orderBy['orderBy']]);
        $_SESSION[self::page]['where'] = $where;
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        $url = root . "control/adIndex.html?cla=" . self::page;
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = [
                "box" => ["name" => "listId", "value" => $array['id']],
            ];
            $liButton[] = $this->dominate($dominate) . hidden(["name" => "cylinderId", "value" => $array['id']]);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
        ));
        //返回
        $json['html'] = "<form name='listForm'>" . $li . "</form>" . $page['button'] .clear;
        return json_encode($json, 320);
    }


    // 操作中转
    public function operate(): bool|string
    {
        $act  = $this->post('act') ?? '';
        $pwd  = false; // 需要密码
        $form = $word = $power = '';
        switch ($act) {
            case 'edit':
                $act      = 'edit';
                $msg      = '修改';
                $title    = '是否检验所选信息？';
                $function = 'edit';
                $form     = self::page . 'OperateForm, listForm';
                $power    = '';
                $word = true;
                break;
            default:
                $act      = 'del';
                $msg      = '删除';
                $title    = '是否删除所选信息，删除后不可找回？';
                $function = 'delete';
                $form     = self::page . 'OperateForm, listForm';
                $power    = '';
                $word = true;
                break;
        }
        if (!$this->power(self::page, $act)) {
            $_SESSION['warn'] = '你还没有' . $msg . '的权限';
            $json             = [
                'warn' => 2,
                'href' => root . 'control/adIndex.html?cla=' . $this->get('cla') . '&fun=home',
            ];
        } else {
            return $this->confirm([
                'title'    => $title,
                'function' => $function,
                'form'     => $form,
                'power'    => $power,
                'pwd'      => $pwd,
                'word'     => $word,
            ]);
        }

        resp:
        return json_encode($json, 320);
    }

    /**
     * 检验
     * @return bool|string
     */
    public function edit()
    {
        $array = $this->post('listId') ?? $this->post('id');
        if (!$this->power(self::page, "edit")) {
            $json['warn'] = '你还没有编辑的权限';
        } elseif (empty($array)) {
            $json['warn'] = '请选择钢瓶信息';
        } else {
            $text = "";
            $x = 0;
            $array = is_array($array) ? $array : [$array];
            foreach ($array as $id) {
                //最后删除基本参数
                $info = query(self::table,"id = '" . $id . "'");
                update(["table" => self::table.'_log' , "field" => [
                    'state' => '已质检',
                ], "where" => "id = '" . $id . "'"]);
                update(["table" => self::table , "field" => [
                    'state' => '空瓶',
                    'lastTime' => $this->time,
                ], "where" => "id = '" . $info['steelCylinderId'] . "'"]);
                //返回
                $warn = '质检成功';
                $x++;
                $text .= '钢瓶信息' . $warn . "。<br>";
            }
            //添加日志
            $this->logText(array("type" => self::page, "text" => $text));
            $_SESSION['warn'] = str_replace(['{{x}}'], [$x],"成功质检{{x}}个钢瓶信息，详情请查看日志");
            $json['warn'] = 2;
            $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=home";
        }
        return json_encode($json, 320);
    }
}